{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "training_surfrider.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SUo-P6BNJ5ES",
        "colab_type": "text"
      },
      "source": [
        "# Launch object detection *training*\n",
        "\n",
        "## Installation\n",
        "\n",
        "You should select a runtime with GPU first: (Runtime -> Change Runtime Type = GPU)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mo3EiovnFvye",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!apt-get install libsm6 libxext6 libxrender-dev libcap-dev ffmpeg\n",
        "!pip install --upgrade git+https://github.com/surfriderfoundationeurope/mot.git\n",
        "!pip install git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oe_tvYuwNult",
        "colab_type": "text"
      },
      "source": [
        "## Training"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YgpwxyWZPQvu",
        "colab_type": "text"
      },
      "source": [
        "### Downloading dataset"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hQbnsrtDPQGh",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import wget\n",
        "from zipfile import ZipFile\n",
        "\n",
        "wget.download(\"http://files.heuritech.com/raw_files/dataset_surfrider_cleaned.zip\", \".\")\n",
        "\n",
        "with ZipFile('dataset_surfrider_cleaned.zip', 'r') as zipObj:\n",
        "     zipObj.extractall()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zcZLBxJz0Qu9",
        "colab_type": "text"
      },
      "source": [
        "### Download pretrained weights on COCO"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tSQahaeC0T_d",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "wget.download(\"http://models.tensorpack.com/FasterRCNN/COCO-MaskRCNN-R50FPN2x.npz\", \".\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rR-D8zghz9r4",
        "colab_type": "text"
      },
      "source": [
        "### Launch the training\n",
        "\n",
        "See this [file](https://github.com/surfriderfoundationeurope/mot/blob/master/src/mot/object_detection/README.md) to choose the architecture you want and the according pre trained weights. The weights downloaded above correspond to ResNet50-FPN with 2X scheduling.  \n",
        "Since we are not interested in segmentation, we set MODE_MASK=False. Also, because the dataset is pretty small, we don't need to train the network for a lot of steps. We decrease the learning rate at steps (250,500,750) * 8 GPUs, correspoding to the steps (2000, 4000, 6000)."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PvNO6o9Yz8DS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!python3 -m mot.object_detection.train --load COCO-MaskRCNN-R50FPN2x.npz --logdir resnet50_fpn --config DATA.BASEDIR=dataset_surfrider_cleaned MODE_MASK=False TRAIN.LR_SCHEDULE=250,500,750"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DgDngrdMDsoR",
        "colab_type": "text"
      },
      "source": [
        "### Visualize predictions"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GEXZ6r18FrzY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!mkdir plastic_trained_resnet50_fpn"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YYdv-q_IEBaR",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "wget.download(\"https://files.heuritech.com/raw_files/surfrider/resnet50_fpn/model-6000.index\", \"plastic_trained_resnet50_fpn\")\n",
        "wget.download(\"https://files.heuritech.com/raw_files/surfrider/resnet50_fpn/model-6000.data-00000-of-00001\", \"plastic_trained_resnet50_fpn\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QM0eCcdTD8L8",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!python3 -m mot.object_detection.predict --load plastic_trained_resnet50_fpn/model-6000 --predict dataset_surfrider_cleaned/Images_md5/9ddc58812851ad643114930524601f10 --config DATA.BASEDIR=dataset_surfrider_cleaned MODE_MASK=False"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Bs-QVy8KG-36",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from IPython.display import Image\n",
        "Image('output.png')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LyGKs4uRahRI",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}